package com.usian.article.controller

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
import com.usian.apis.article.AuthorControllerApi
import com.usian.article.service.impl.AuthorServiceImpl
import com.usian.model.article.pojos.ApAuthor
import com.usian.model.common.dtos.ResponseResult
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/api/v1/author")
class AuthorController : AuthorControllerApi {
    @Autowired
    private lateinit var authorService: AuthorServiceImpl;

    @GetMapping("/findByUserId/{id}")
    override fun findByUserId(@PathVariable id: Int): ApAuthor? {
        return if (id != null) authorService!!.getOne(QueryWrapper<ApAuthor>().eq("id", id)) else null
    }


    @PostMapping("/save")
    override fun save(@RequestBody apAuthor: ApAuthor): ResponseResult<*> {
        return if (apAuthor != null) {
            authorService!!.save(apAuthor)
            ResponseResult.okResult(apAuthor)
        } else authorService.paramError
    }

    @GetMapping("/one/{id}")
    override fun findById(@PathVariable id: Int?): ApAuthor? {
        return if (id == null) null;
        else authorService.getById(id)
    }
}